package com.liankuan.console.repository;

import com.liankuan.console.dto.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;

@Component
public interface UserRepository extends JpaRepository<User, Integer> {
    User findByNameAndPassword(String name, String password);

    User findByToken(String token);

    User findByName(String name);

    @Override
    Page<User> findAll(Pageable pageable);

    @Query(value = "update user set name=?2,password=?3 where id=?1 ", nativeQuery = true)
    @Modifying
    void updateUserInfo(@Param(value = "id") Integer id, @Param(value = "name") String name, @Param(value =
            "password") String password);

    /**
     * 更新Token
     *
     * @param id
     * @param token
     */
    @Modifying
    @Transactional
    @Query(value = "update user set token=?2 where id=?1", nativeQuery = true)
    void updateUserToken(Integer id, String token);
}
